package com.cuitrip.business.track;

import android.text.TextUtils;

import com.cuitrip.apiservice.TrackBusiness;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.ArrayUtil;

import java.util.HashMap;
import java.util.List;

/**
 * 行程数据代理类
 * Created by ak on 15/9/23.
 */
public class TrackProxy implements IProxyCallback{


    private static TrackProxy sInstance;

    private ApiProxy apiProxy = new ApiProxy(this);

    public static TrackProxy getInstance() {
        if (null == sInstance)
            sInstance = new TrackProxy();
        return sInstance;
    }

    public void requestTrack(HashMap<String, Object> dataParams) {
        TrackBusiness.request(apiProxy, dataParams);
    }


    public void reviewPage(TrackBusiness.Source source, String id) {
        HashMap<String, Object> dataParams = new HashMap<>();
        dataParams.put(TrackBusiness.SOURCE, source);
        dataParams.put(TrackBusiness.OP, TrackBusiness.Action.REVIEW);
        if (!TextUtils.isEmpty(id)){
            dataParams.put(TrackBusiness.ITEM_ID, id);
        }
        requestTrack(dataParams);
    }


    public void operationAction(TrackBusiness.Source source, TrackBusiness.Action operationAction, String page, String index, List<String> itemIds) {
        HashMap<String, Object> dataParams = new HashMap<>();
        dataParams.put(TrackBusiness.SOURCE, source);
        dataParams.put(TrackBusiness.OP, operationAction);
        if (!TextUtils.isEmpty(page)) {
            dataParams.put(TrackBusiness.PAGE, page);
        }
        if (!TextUtils.isEmpty(index)) {
            dataParams.put(TrackBusiness.OPERATION_INDXE, index);
        }
        if (!ArrayUtil.isNullOrEmpty(itemIds)) {
            String ids = TextUtils.join(",",itemIds);
            dataParams.put(TrackBusiness.ITEM_ID, ids);
        }
        requestTrack(dataParams);
    }

    public void searchWords(TrackBusiness.Source source, String keyWord, String sourceWord) {
        HashMap<String, Object> dataParams = new HashMap<>();
        dataParams.put(TrackBusiness.SOURCE, source);
        dataParams.put(TrackBusiness.OP, TrackBusiness.Action.SEARCH);
        if (!TextUtils.isEmpty(keyWord)) {
            dataParams.put(TrackBusiness.KEY_WORD, keyWord);
        }
        if (!TextUtils.isEmpty(sourceWord)) {
            dataParams.put(TrackBusiness.HOT_WORD, sourceWord);
        }
        requestTrack(dataParams);
    }

    public String getCurrentPageIndex(int pageSize, int position) {
        return String.valueOf(position);
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        return true;
    }
}
